﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using PagedList;
using TestOnlineProject.Models.BusinessLogic;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Controllers
{
    [Authorize]
    public class FeedbackController : Controller
    {
        FeedbackBLO feedbackBLO = new FeedbackBLO();
        //
        // GET: /Admin/Feedback/

        [Authorize(Roles = "1")]
        public ActionResult Index(int? page, string keyWord = "", string column = "", string sort = "")
        {
            try
            {
                // No. list.
                var noList = new List<NoModel>();

                // Violation list.
                List<Feedback> violations = feedbackBLO.ToList(ref noList, page, keyWord, column, sort);

                // Sort states.
                ViewBag.KeyWord = keyWord;
                ViewBag.Page = page;
                ViewBag.Column = column;
                ViewBag.Sort = sort;
                ViewBag.NoList = noList;

                // Return.
                var pageNumber = page ?? 1;
                var onePageOfViolations = violations.ToPagedList(pageNumber, 10);
                ViewBag.onePageOfViolations = onePageOfViolations;
                return Request.IsAjaxRequest()
                    ? (ActionResult)PartialView("_List")
                    : View();
            }
            catch (Exception)
            {
                // Wrtite to log file.
                return RedirectToAction("Index", "Error", new { Area = "" });
            }
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Feedback feedback)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    feedback.UserId = int.Parse(HttpContext.User.Identity.Name);
                    var result = feedbackBLO.Add(feedback);
                    if (result > 0)
                    {
                        TempData["Msg"] = "Send successfully. Thanks for your feedback.";
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "An error occurred.");
                    }
                }
                return View(feedback);
            }
            catch (Exception)
            {
                // Write to log file.
                return RedirectToAction("Index", "Error");
            }
        }

        [Authorize(Roles = "1")]
        public ActionResult Details(int id = 0, string isChecked = "")
        {
            var feedback = feedbackBLO.Find(id);
            if (feedback == null)
            {
                return HttpNotFound();
            }
            if (isChecked.Equals("checked"))
            {
                feedbackBLO.Check(id + "-");
            }
            return View(feedback);
        }

        [Authorize(Roles = "1")]
        public string Uncheck(string ids = "")
        {
            try
            {
                int result = feedbackBLO.Uncheck(ids);
                if (result > 0)
                {
                    if (result == 1)
                    {
                        return "The feedback has been marked as unread.";
                    }
                    else
                    {
                        return result + " feedbacks has been marked as unread.";
                    }
                }
                else
                {
                    return "An error occurred.";
                }
            }
            catch (Exception)
            {
                // Write to log file.
                return "An error occurred.";
            }
        }

        [Authorize(Roles = "1")]
        public string Check(string ids = "")
        {
            try
            {
                int result = feedbackBLO.Check(ids);
                if (result > 0)
                {
                    if (result == 1)
                    {
                        return "The feedback has been marked as read.";
                    }
                    else
                    {
                        return result + " feedbacks has been marked as read.";
                    }
                }
                else
                {
                    return "An error occurred.";
                }
            }
            catch (Exception)
            {
                // Write to log file.
                return "An error occurred.";
            }
        }

        [Authorize(Roles = "1")]
        public string Delete(string ids = "")
        {
            try
            {
                int result = feedbackBLO.Remove(ids);
                if (result > 0)
                {
                    if (result == 1)
                    {
                        return "The feedback has been deleted.";
                    }
                    else
                    {
                        return result + " feedbacks has been deleted.";
                    }
                }
                else
                {
                    return "An error occurred.";
                }
            }
            catch (Exception)
            {
                // Write to log file.
                return "An error occurred.";
            }
        }
    }
}
